package com.quasar.hpatient.utils;

import android.content.ContentValues;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.model.litePalmodel.Recorddb;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;

import org.litepal.crud.DataSupport;

import java.util.Date;
import java.util.List;

/**
 * author : 许建宏
 * Date: 2018/1/24 0024
 * View:
 */

public class motionUtils {

    static PatientInfoData patientInfoData= MainAppliaction.getInstance().getPatient();

    public static void Addmotion(String yundong){

        String time=TimeZoneUtil.sdfFormat.format(new Date());
        List<Recorddb> listrecode= DataSupport.findAll(Recorddb.class);
        int num = listrecode.size();

        if(num==0){
            Recorddb recorddb = new Recorddb(
                    1,
                    patientInfoData.getId()+"",
                    patientInfoData.getId()+"",
                    time, yundong);
            recorddb.save();
        }

        for(int i=0;i<num;i++){
            if(listrecode.get(i).getAddDate().equals(time)){
                ContentValues cv = new ContentValues();
                cv.put("yundong",yundong);
                DataSupport.update(Recorddb.class, cv, listrecode.get(i).getId());
            }else{
                Recorddb recorddb = new Recorddb(
                        1,
                        patientInfoData.getId()+"",
                        patientInfoData.getId()+"",
                        time, yundong);
                recorddb.save();
            }
        }
    }



    //查询到当前的数据
    public static Recorddb recordList(){
        Recorddb recorddb=null;
        String time=TimeZoneUtil.sdfFormat.format(new Date());
        List<Recorddb> listrecode= DataSupport.findAll(Recorddb.class);
        int num = listrecode.size();
        for(int i=0;i<num;i++) {
            if (listrecode.get(i).getAddDate().equals(time)) {
                recorddb=listrecode.get(i);
                break;
            }
        }

        if(recorddb==null){
            Addmotion("");
        }
        return recorddb;
    }
}
